#include "LoadingScene.h"
#include "AppDelegate.h"
#include "MainMenuScene.h"
#include "CMyGameDef.h"

using namespace cocos2d;

#define LINE_SPACE	40

CCScene* LoadingScene::scene()
{
    CCScene * scene = NULL;
    do 
    {
        // 'scene' is an autorelease object
        scene = CCScene::create();
        CC_BREAK_IF(! scene);

        // 'layer' is an autorelease object
        LoadingScene *layer = LoadingScene::create();
        CC_BREAK_IF(! layer);

        // add layer as a child to scene
        scene->addChild(layer);
    } while (0);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool LoadingScene::init()
{
    bool bRet = false;
    do 
    {
        //////////////////////////////////////////////////////////////////////////
        // super init first
        //////////////////////////////////////////////////////////////////////////

        CC_BREAK_IF(! CCLayer::init());

        //////////////////////////////////////////////////////////////////////////
        // add your codes below...
        //////////////////////////////////////////////////////////////////////////
		
		/*
        // 1. Add a menu item with "X" image, which is clicked to quit the program.
		
        // Create a "close" menu item with close icon, it's an auto release object.
        CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage(
            "CloseNormal.png",
            "CloseSelected.png",
            this,
            menu_selector(HelloWorld::menuCloseCallback));
        CC_BREAK_IF(! pCloseItem);

        // Place the menu item bottom-right conner.
        pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));

        // Create a menu with the "close" menu item, it's an auto release object.
        CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL);
        pMenu->setPosition(CCPointZero);
        CC_BREAK_IF(! pMenu);

        // Add the menu to HelloWorld layer as a child layer.
        this->addChild(pMenu, 1);
		*/

		// Get window size
        CCSize size = CCDirector::sharedDirector()->getWinSize();

		// add labeled menu
		// start menu
		CCSprite* pSprite = CCSprite::create("Images/logo.png");
		pSprite->setAnchorPoint(ccp(0.5, 0.5));
		pSprite->setPosition(ccp(size.width/2, size.height/2 + LINE_SPACE));

		CCLabelTTF *ploadingLabel = CCLabelTTF::create("Loading ...", "Arial", 16);
		ploadingLabel->setPosition(ccp(size.width/2, size.height/2 - LINE_SPACE));
		ploadingLabel->setColor(ccWHITE);

        // Add the menu to ManiMenuScene layer as a child layer.
        this->addChild(pSprite, 1);
		this->addChild(ploadingLabel, 1);

		/*
        // 3. Add add a splash screen, show the cocos2d splash image.
        CCSprite* pSprite = CCSprite::spriteWithFile("HelloWorld.png");
        CC_BREAK_IF(! pSprite);

        // Place the sprite on the center of the screen
        pSprite->setPosition(ccp(size.width/2, size.height/2));

        // Add the sprite to HelloWorld layer as a child layer.
        this->addChild(pSprite, 0);
		*/
		m_nLoadingStep = 0;
		m_nPercent = 0;
		schedule(schedule_selector(LoadingScene::updateGame));
        bRet = true;
    } while (0);
    return bRet;
}

void LoadingScene::updateGame(float dt)
{
	m_nPercent = AppDelegate::loadCommonResource(m_nLoadingStep);

	//drawLoadingBar (n);
	if (m_nPercent == 100)
	{
		CCScene *pScene = MainMenuScene::scene();
		CCDirector::sharedDirector()->replaceScene(pScene);
	}
	m_nLoadingStep ++;
}

#define LOADING_BAR_WIDTH	200
#define LOADING_BAR_HEIGHT	20

void LoadingScene::drawLoadingBar(int nPercent)
{
	CCSize size = CCDirector::sharedDirector()->getWinSize();
	CCPoint pntRect[4];

	glLineWidth(1);

	// bounds
	pntRect[0].x = size.width/2 - LOADING_BAR_WIDTH/2;
	pntRect[0].y = size.height/2 - LINE_SPACE - 20;
	pntRect[1].x = size.width/2 + LOADING_BAR_WIDTH/2;
	pntRect[1].y = pntRect[0].y;
	pntRect[2].x = pntRect[1].x;
	pntRect[2].y = pntRect[1].y - LOADING_BAR_HEIGHT;
	pntRect[3].x = pntRect[0].x;
	pntRect[3].y = pntRect[2].y;
	glColor4f(0.0, 0.25, 0.5, 1.0);
	ccDrawPoly(pntRect, 4, true);
	// loading gauge
	pntRect[0].x = size.width/2 - LOADING_BAR_WIDTH/2;
	pntRect[0].y = size.height/2 - LINE_SPACE - 20-1;
	pntRect[1].x = size.width/2 - LOADING_BAR_WIDTH/2 + LOADING_BAR_WIDTH * nPercent / 100-1;
	pntRect[1].y = pntRect[0].y;
	pntRect[2].x = pntRect[1].x;
	pntRect[2].y = pntRect[1].y - LOADING_BAR_HEIGHT+1;
	pntRect[3].x = pntRect[0].x;
	pntRect[3].y = pntRect[2].y;
	//glColor4f(0.0, 0.5, 1.0, 1.0);
	ccDrawSolidPoly(pntRect, 4, ccc4f(0.0, 0.5, 1.0, 1.0));
}

void LoadingScene::draw(void)
{
	CCLayer::draw();
	drawLoadingBar(m_nPercent);
}
